package com.easemob.helpdesk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.helpdesk.widget.SwitcherButton;
import com.housekeeperapp.MainApplication;
import com.housekeeperapp.R;
import com.net.ApiService;
import com.net.HKHttpUtils;
import com.net.HKResponseResultCallBack;

import retrofit2.Call;

/**
 * Created by xiaolong.wei on 2019/6/11.
 */

public class ChangeServiceStatusActivity extends BaseActivityV2 {
    private static final String TAG = "ChangeServiceStatusActi";

    public static final int REQUEST_OK = 0x11;

    LinearLayout back;
    ImageView statusImage;
    TextView text;
    SwitcherButton button;

    private int isRest;//0工作中1表示休息中

    @Override
    protected void initFindView() {

        back=findViewById(R.id.back);
        statusImage=findViewById(R.id.status_image);

        text=findViewById(R.id.text);

        button=findViewById(R.id.button);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_service_status);
        initFindView ();
        isRest = getIntent().getIntExtra("isRest",0);
        Log.d(TAG, "onCreate: "+isRest);
        button.setState(isRest==1?0:1);
        changeStatus(isRest==1?0:1);
        button.setOnChangeStateListener(new SwitcherButton.OnChangeStateListener() {

            @Override
            public void beforeChange(int state) {
                isRest = state;
                Log.d(TAG, "beforeChange: isRest= "+isRest);
                if (state == 1){
                    //出弹窗提示
                    showDialog("关闭“服务中”状态，您将进入休息模式，无法接收订单");
                }else {
                    //出弹窗提示
                    showDialog("打开“服务中”状态，会收到系统派单");
                }

            }

            @Override
            public void onStateChanged(int state) {
                changeStatus(state);
            }
        });


    }

    private void showDialog(String msg){
        Intent intent = new Intent();
        intent.setClass(ChangeServiceStatusActivity.this, AlertDialog.class);
        intent.putExtra("msg", msg);
        intent.putExtra("okString", "确定");
        intent.putExtra("cancelString","取消");
        startActivityForResult(intent, REQUEST_OK);
    }



    private void changeStatus(int state){
        if (state == 0){
            statusImage.setImageResource(R.drawable.service_status_resting);
            text.setText("休息中");
        }else {
            statusImage.setImageResource(R.drawable.service_status_working);
            text.setText("服务中");
        }
        MainApplication.getInstance().getEventEmitter().sendEvent("update_session");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_OK && resultCode == RESULT_OK){
            postData();
        }else if (requestCode == REQUEST_OK && resultCode == RESULT_CANCELED){

        }
    }


    private void postData(){
        ApiService apiService = HKHttpUtils.getApiService();
        apiService.changeRestStatus(MainActivity.ticket,isRest).enqueue(new HKResponseResultCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                button.updateState();
            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                Toast.makeText(ChangeServiceStatusActivity.this,msg,Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailed(Call call, Throwable t) {
                Toast.makeText(ChangeServiceStatusActivity.this,"网络连接异常",Toast.LENGTH_LONG).show();
            }
        });
    }
}
